/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

package au.com.lastweekend.jim.model;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;

import au.com.lastweekend.jim.imagebase.ImageManager;

/**
 * @author grant@lastweekend.com.au
 * @version $Id: ImageDirectoryModel.java,v 1.9 2006/02/19 06:04:54 ggardner Exp $
 */
public final class ImageDirectoryModel extends AbstractContactSheetListModel<ContactSheetListItem> {

    private static final long serialVersionUID = -7779420613843576890L;

    private File directory;

    private ImageManager imageManager;
    private static final Logger LOG = Logger.getLogger(ImageDirectoryModel.class);

    public ImageDirectoryModel(File directory, ImageManager imageManager) {

        super();
        this.imageManager = imageManager;

        try {
            directory = directory.getCanonicalFile();
        } catch (IOException e) {
            LOG.warn("Unable to canonicalize directory " + directory.getPath(), e);
        }

        this.directory = directory;
        reload();

    }

    public void reload() {

        List<ContactSheetListItem> c = new ArrayList<ContactSheetListItem>();

        c.addAll(ContactSheetDirectoryItem.makeList(getDirectories(directory)));
        c.addAll(ContactSheetImageItem.makeList(imageManager.getDirectoryImages(directory)));

        Collections.sort(c);
        setListItems(c);
    }

    /**
     * @param dirFile
     * @return
     */
    private List<File> getDirectories(File dirFile) {

        List<File> directories = new ArrayList<File>();

        File thisFile = new File(dirFile, ".");
        directories.add(thisFile);
        File parent = new File(dirFile, "..");
        directories.add(parent);
        File[] children = dirFile.listFiles(new FileFilter() {

            public boolean accept(File pathname) {

                return pathname.isDirectory();
            }
        });
        
        if (children != null) {
            //It is possible for the model to be reloaded by the worker thread
            //after the directory has been deleted!
            directories.addAll(Arrays.asList(children));
        }

        return directories;
    }

    /*
     * @see au.com.lastweekend.jim.ui.ContactSheetListModel#getName()
     */
    public String getName() {

        try {
            return directory.getCanonicalFile().getName();
        } catch (IOException e) {
            LOG.error("Can't get canonical file for " + directory, e);
            return directory.getName();

        }
    }

    public File getDirectory() {

        return directory;
    }

    public void setDirectory(File directory) {

        this.directory = directory;
        reload();
    }
}